#include<bits/stdc++.h>
using namespace std;
int n;
int a[200005];
int af[200005];
int la[1000005];
int dfs(int w,int r,int b)
{
    if(w==n)
    {
        return 0;
    }
    if(af[w]==0)
    {
        if(a[w]==r&&a[w]==b)
        {
            return dfs(w+1,r,b)+a[w];
        }
        if(a[w]==r)
        {
            return dfs(w+1,a[w],b)+a[w];
        }
        if(a[w]==b)
        {
            return dfs(w+1,r,a[w])+a[w];
        }
    }
    int t=dfs(w+1,a[w],b)+(a[w]==r)*a[w];
    int t1=dfs(w+1,r,a[w])+(a[w]==b)*a[w];
    return max(t,t1);
}
int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n;
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(int i=n-1;i>=0;i--)
        {
            if(la[a[i]]!=0)
            {
                af[i]=a[i];
            }
            else
            {
                af[i]=0;
            }
            la[a[i]]=i;
        }
        for(int i=n-1;i>=0;i--)
        {
            if(la[a[i]]!=0)
            {
                af[i]=a[i];
            }
            else
            {
                af[i]=0;
            }
            la[a[i]]=i;
        }
        cout<<dfs(0,-1,-1)<<endl;
    }
}
